/*
 * Copyright (c) 2023 iQi.Co.Ltd. All rights reserved.
 */
 
using IQIGame.Onigao.Framework;
using System.Collections.Generic;
using System.Linq;
using System;
using NetProtocol.Enum;

namespace NetProtocol.POD {

	/// <summary>
	/// 邮件
	/// module : structs
	/// </summary>
	public class MailPOD : IPOD 
    {
        public readonly static Func<MailPOD> FACTORY = () => new MailPOD();
		public static Func<MailPOD> DynamicFactory = FACTORY;
        
		/*-- members --*/
		/// <summary>
        /// 
        /// </summary>
		public long id;
		/// <summary>
        ///  配置id
        /// </summary>
		public int cid;
		/// <summary>
        ///  标题 
        /// </summary>
		public string title;
		/// <summary>
        ///  内容
        /// </summary>
		public string content;
		/// <summary>
        ///  发送者
        /// </summary>
		public string sender;
		/// <summary>
        ///  是否已读
        /// </summary>
		public bool isRead;
		/// <summary>
        ///  是否领取过附件
        /// </summary>
		public bool isGotItem;
		/// <summary>
        ///  附件列表, 领取后也不会删除
        /// </summary>
        private List<ItemShowPOD> m_itemList;
		/// <summary>
        ///  发送时间(毫秒时间戳)
        /// </summary>
		public long createTime;
		/// <summary>
        ///  过期时间(毫秒时间戳) 0代表永久
        /// </summary>
		public long expireTime;
		
		
        public List<ItemShowPOD> itemList
        {
            get
            {
                if(m_itemList == null)
                {
                    m_itemList = new();
                }
                return m_itemList;
            }
            private set => m_itemList = value;
        }
        public List<ItemShowPOD> unsafe_itemList
        {
        	get => m_itemList;
        	set => m_itemList = value;
        }
		
		/*-- constructors --*/
		public MailPOD() {}
		public MailPOD(long id, int cid, string title, string content, string sender, bool isRead, bool isGotItem, List<ItemShowPOD> itemList, long createTime, long expireTime) {
			this.id = id;
			this.cid = cid;
			this.title = title;
			this.content = content;
			this.sender = sender;
			this.isRead = isRead;
			this.isGotItem = isGotItem;
			this.m_itemList = itemList;
			this.createTime = createTime;
			this.expireTime = expireTime;
		}
	      public void Serial(SOutputStream _out) {
            _out.Write_long(id);
            _out.Write_int(cid);
            _out.Write_string(title);
            _out.Write_string(content);
            _out.Write_string(sender);
            _out.Write_boolean(isRead);
            _out.Write_boolean(isGotItem);
            _out.Write_list_pod(m_itemList);
            _out.Write_long(createTime);
            _out.Write_long(expireTime);
        }
        
        public void UnSerial(SInputStream _in) {
            if(!_in.HasRemaining()) return;
            id = _in.Read_long();
            if(!_in.HasRemaining()) return;
            cid = _in.Read_int();
            if(!_in.HasRemaining()) return;
            title = _in.Read_string();
            if(!_in.HasRemaining()) return;
            content = _in.Read_string();
            if(!_in.HasRemaining()) return;
            sender = _in.Read_string();
            if(!_in.HasRemaining()) return;
            isRead = _in.Read_boolean();
            if(!_in.HasRemaining()) return;
            isGotItem = _in.Read_boolean();
            if(!_in.HasRemaining()) return;
            m_itemList = _in.Read_list_pod<ItemShowPOD>(ItemShowPOD.DynamicFactory);
            if(!_in.HasRemaining()) return;
            createTime = _in.Read_long();
            if(!_in.HasRemaining()) return;
            expireTime = _in.Read_long();
        }
		
		public MailPOD Clone() {
            MailPOD o = DynamicFactory();
            o.id = id;
            o.cid = cid;
            o.title = title;
            o.content = content;
            o.sender = sender;
            o.isRead = isRead;
            o.isGotItem = isGotItem;
            if (m_itemList != null) {
                o.m_itemList = m_itemList.ConvertAll(o => o?.Clone());
            }
            o.createTime = createTime;
            o.expireTime = expireTime;
            return o;
        }
        
        public void Clear() {
            id = default;
            cid = default;
            title = default;
            content = default;
            sender = default;
            isRead = default;
            isGotItem = default;
            m_itemList = default;
            createTime = default;
            expireTime = default;
        }
		
        public override string ToString() {
			string str = "";
			str += "MailPOD [";
			str += "id=";
			str += id;
			str += ", cid=";
			str += cid;
			str += ", title=";
			str += title;
			str += ", content=";
			str += content;
			str += ", sender=";
			str += sender;
			str += ", isRead=";
			str += isRead;
			str += ", isGotItem=";
			str += isGotItem;
			str += ", itemList=";
			str += m_itemList.ToString<ItemShowPOD>();
			str += ", createTime=";
			str += createTime;
			str += ", expireTime=";
			str += expireTime;
			str += "]";
			return str;
		}
		
	}
}